package com.hq.util;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.UUID;

public class FileUploadUtils {

    // 上传
    public static String  load(HttpSession session, MultipartFile file, String toPath) {
        String originalFilename = file.getOriginalFilename();
        int indexOf = originalFilename.lastIndexOf(".");
        String houzhui = originalFilename.substring(indexOf);
        String string = UUID.randomUUID().toString();
        String realPath = session.getServletContext().getRealPath("/"+toPath);
        String path=realPath+ File.separator+string+houzhui;
        // 上传文件
        File file2 = new File(path);
        try {
            file.transferTo(file2);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return toPath+File.separator+string+houzhui;
    }


    // 下载
    public static  void down(String fileName, String toPath, HttpServletResponse response, HttpServletRequest request) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("multipart/form-data");
        // 设置下在的头文件  格式
        response.setHeader("Content-Disposition", "attachment;fileName="
                + fileName);
        // 获取文件的 绝对路径
        String realPath = request.getSession().getServletContext().getRealPath("/"+toPath+"/");
        realPath=realPath+File.separator+fileName;
        try {
            FileInputStream fis=new FileInputStream(realPath);
            OutputStream outputStream = response.getOutputStream();
            int read=0;
            while((read=fis.read())!=-1) {
                outputStream.write(read);
            }

            close(fis,outputStream);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public  static void  close(InputStream is, OutputStream os) {
        try {
            if(is!=null) {
                is.close();
            }
            if(os!=null) {
                os.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
